<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>props</title>
</head>

<body>
    <h1>考试编程题1</h1>
    <ol>
        <li>在父组件中定义p标签。</li>
        <li>在子组件A定义文本框和按钮。</li>
        <li>点击A组件的按钮，在父组件的p标签中展示A组件中文本框的值。</li>
    </ol>
    <div id="app"></div>

    <script src="../node_modules/react/umd/react.development.js"></script>
    <script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
    <script src="../node_modules/babel-standalone/babel.min.js"></script>
    <script type="text/babel">

        function ParentCom() {

            const [text,setText] = ReactDOM.useState("");

            return (
                <div>
                    <h2>我是父组件</h2>
                    <p>{text}</p>
                    <ChildCom setText={setText}/>
                </div>
            )
        }

        function ChildCom(props) {

            const textRef = useRef();

            function handleClick() {
                const value = textRef.current.value;
                props.setText(value);
            }

            return (
                <div>
                    <h2>我是子组件</h2>
                    <input type="text" ref={textRef}/>
                    <button onClick={handleClick}>提交</button>
                </div>
            )
        }

        const app = document.querySelector("#app");
        const root = ReactDOM.createRoot(app);
        root.render(<ParentCom/>);
    </script>
</body>

</html>